package com.china08.yxyapi.api.manage.tec.teaching.lesson.school;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsSchoolFaBuController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonRepository lessonRepository;

    @Autowired
    YxyLessonsSchoolLessonChapterRepository chapterRepository;

    @Autowired
    YxyLessonsSchoolLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsSchoolCatalogRepository catalogRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/school/lesson/fabu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        YxyLessonsSchoolLesson lesson = lessonRepository.findById(lessonId);

        if (lesson != null) {
            // 获取课程对应的栏目
            String catalogId1 = lesson.getCatalogId1();
            String catalogId2 = lesson.getCatalogId2();

            // 一级栏目
            YxyLessonsSchoolCatalog catalog1 = catalogRepository.findById(catalogId1);
            Assert.isTrue(catalog1 != null, "当前课程一级栏目为空");
            // 二级栏目
            YxyLessonsSchoolCatalog catalog2 = catalogRepository.findById(catalogId2);
            Assert.isTrue(catalog2 != null, "当前课程二级栏目为空");

            // 课程为待审核
            lesson.setStatus(1);
            // 设置最后一次修改时间
            Date date = new Date();
            lesson.setLastModifiedDate(new Timestamp(date.getTime()));
            // 将创建时间改为当前系统时间
            lesson.setCreateDate(date);

            List<YxyLessonsSchoolLessonChapter> chapters = chapterRepository.findByLessonIdOrderByOrderNum(lessonId);
            Assert.isTrue(chapters.size() > 0, "请添加章节");

            for (YxyLessonsSchoolLessonChapter chapter : chapters) {
                String chapterId = chapter.getId();
                List<YxyLessonsSchoolLessonItem> items = itemRepository.findByChapterIdAndStatusNotOrderByOrderNum(chapterId, -1);
                Assert.isTrue(items.size() > 0, "请在章节下添加课时");
            }

            for (YxyLessonsSchoolLessonChapter chapter : chapters) {
                String chapterId = chapter.getId();
                List<YxyLessonsSchoolLessonItem> items = itemRepository.findByChapterIdAndStatusNotOrderByOrderNum(chapterId, -1);
                for (YxyLessonsSchoolLessonItem item : items) {
                    if (item.getStatus() != 2) {
                        item.setStatus(1);
                        itemRepository.save(item);
                    }
                }
            }
            lessonRepository.save(lesson);
        }
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "课程不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }
}
